package com.community.service.impl;

import com.community.annotation.SysLog;
import com.community.base.BaseService;
import com.community.common.Const;
import com.community.common.ServerResponse;
import com.community.pojo.Label;
import com.community.pojo.QuestionLabel;
import com.community.service.ILabelService;
import com.community.util.NumberUtil;
import com.community.util.StringUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 标签服务层
 */
@Service("iLabelService")
public class LabelServiceImpl extends BaseService implements ILabelService {

    /**
     * 新增标签
     * @param name
     * @param request
     * @return
     */
    @SysLog("新增标签")
    @CacheEvict(value = "selectAllLabel",allEntries=true)
    public ServerResponse<String> add(String name, HttpServletRequest request){
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null == o){
            return ServerResponse.createByErrorMessage(Const.Msg.USERNOLOGIN);
        }
        Label label = new Label();
        label.setName(name);//标签名称
        if(labelMapper.insert(label) <= 0){
            return ServerResponse.createByErrorMessage(Const.Msg.SAVEERROR);
        }
        return ServerResponse.createBySuccessMessage(Const.Msg.SAVESUCCESS);
    }

    /**
     * 删除标签
     * @param id
     * @param request
     * @return
     */
    @CacheEvict(value = "selectAllLabel",allEntries=true)
    @SysLog("删除标签")
    public ServerResponse<String> del(int id,HttpServletRequest request){
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null == o){
            return ServerResponse.createByErrorMessage(Const.Msg.USERNOLOGIN);
        }
        if (id > 0) {
            if(labelMapper.deleteByPrimaryKey(id) >0){
                return ServerResponse.createBySuccessMessage(Const.Msg.DELSUCCESS);
            }
        }
        return ServerResponse.createByErrorMessage(Const.Msg.PARAMTERERROR);
    }

    /**
     * 查询所有标签
     * @return
     */
    @Cacheable("selectAllLabel")
    @SysLog("查询所有标签")
    public ServerResponse<List<Map>> selectAllLabel(){
        List<Map> resultList = labelMapper.selectAllLabel();
        if(null != resultList && 0 < resultList.size()){
            return ServerResponse.createBySuccess(Const.Msg.SELECTSUCCESS,resultList);
        }
        return ServerResponse.createBySuccessMessage(Const.Msg.SELECTERROR);
    }


}
